package hibernate.web;

import hibernate.fileControler.MyException;
import hibernate.model.Hatacozumu;
import hibernate.model.Sirketprofil;
import hibernate.service.EmmyService;

import java.util.Collections;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@SuppressWarnings("serial")
@Component("HataCozumuBean")
@Scope("session")
public class HataCozumuBean implements BeanService {

	@Autowired
	private transient EmmyService emmyService;
	
	private Hatacozumu selectedHataCozum;	
	private List<Hatacozumu> hataCozumList;
	
	private TemsilciBean temBean;
	private boolean hataCzIslem =false;
	
	@SuppressWarnings("unchecked")
	@Override
	@PostConstruct
	public void init() {
		hataCozumList = (List<Hatacozumu>) emmyService.findAll(new Hatacozumu());
		Collections.reverse(hataCozumList);
	}

	@Override
	public String restart() {
		init();
		return "list";
	}

	@Override
	public String deleteView() {
		emmyService.delete(selectedHataCozum);
		return "list";
	}

	@Override
	public String viewNew() {
		selectedHataCozum= new Hatacozumu();
		temBean = (TemsilciBean) emmyService.getBean(new TemsilciBean());
		selectedHataCozum.setSirketprofil(temBean.getCurrentTemsilci().getSirketprofil());
		hataCzIslem =true;
		return "detail";
	}

	@Override
	public String viewDetail() { /*
		List<Hatacozumu> valueList = (List<Hatacozumu>) hataCozumTable.getValue();	
		System.out.println("table size : "+valueList.size());		
		Iterator<SimpleRowKey> keys = hataCozumTable.getSelection().getKeys();
		
		if(keys.hasNext()){
			currentHataCozum= valueList.get(keys.next().intValue());
			System.out.println("dolu = "+currentHataCozum.getHtid());
			return "detail";
		}else{
			System.out.println("bo�ta");
			return null;
		}*/ return"detay";
	}

	@Override
	public String SaveOrUpdate() throws MyException {
		emmyService.saveOrUpdate(selectedHataCozum);
		return "";
	}
	
	public void hataCozumIslem(boolean islem){
        
        FacesMessage msg = null; 
		if(islem){
			if(selectedHataCozum !=null){
				hataCzIslem = islem;
			}else{
				hataCzIslem =false;
				msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Hata", "Seçim yapılmadı");
				FacesContext.getCurrentInstance().addMessage(null, msg);  
			}
		}else{
			hataCzIslem = islem;
		}
	}
	
	@SuppressWarnings("unchecked")
	public void getSirketHataCozum(Sirketprofil sp){
		hataCozumList = (List<Hatacozumu>) emmyService.getQuery("findHataCozumUretid", "hUreticiid", sp.getFirmaid());
	}
	
	public String verifyProducer(ComponentSystemEvent event){
		try{
			System.out.println("Hata Çözüm verifyingProducer");
			temBean = (TemsilciBean) emmyService.getBean(new TemsilciBean());
			getSirketHataCozum(temBean.getCurrentTemsilci().getSirketprofil());
			System.out.println("Hata Çözüm verifyProducer ok");
			return "";
		}catch(Exception e){
			System.out.println("Hata Çözüm işlem sayfası erişim hatası: "+e);
			return "index";
		}
	}
	
	@SuppressWarnings("unchecked")
	public String getDetayliHataCozum(){		
		hataCozumList = (List<Hatacozumu>) emmyService.getQuery(createQuery());
		return "list";
	}
	
	@Override
	public String createQuery() {
		Hatacozumu htc  = selectedHataCozum;		
		String query = "from Hatacozumu";				
		
		if(htc == null){
			return query;
		}
		if(htc.getSirketprofil().getFirmaid() != 0){
			if(query.indexOf("where") != -1){
				query += " and ureticiid="+htc.getSirketprofil().getFirmaid()+"";
				
			}
			else{
				query += " where ureticiid="+htc.getSirketprofil().getFirmaid()+"";			
			}
		}
		if(htc.getAygit().getAygitid() != 0){
			if(query.indexOf("where") != -1){
				query += " and aygitid="+htc.getAygit().getAygitid()+"";
				
			}
			else{
				query += " where aygitid="+htc.getAygit().getAygitid()+"";			
			}
		}
		if(htc.getHataKodu() != null){
			if(query.indexOf("where") != -1){
				query += " and hataKodu="+htc.getHataKodu()+"";
				
			}
			else{
				query += " where hataKodu="+htc.getHataKodu()+"";			
			}
		}
		if(htc.getCozumAciklamasi() != null){
			if(query.indexOf("where") != -1){
				query += " and cozumAciklamasi like '%"+htc.getCozumAciklamasi()+"%'";
				
			}
			else{
				query += " where cozumAciklamasi like '%"+htc.getCozumAciklamasi()+"%'";			
			}
		}
		return query;
	}

	@Override
	public void getSession() {
		// TODO Auto-generated method stub
		
	}

	public Hatacozumu getSelectedHataCozum() {
		return selectedHataCozum;
	}

	public void setSelectedHataCozum(Hatacozumu selectedHataCozum) {
		this.selectedHataCozum = selectedHataCozum;
	}

	public List<Hatacozumu> getHataCozumList() {
		return hataCozumList;
	}

	public void setHataCozumList(List<Hatacozumu> hataCozumList) {
		this.hataCozumList = hataCozumList;
	}

	public TemsilciBean getTemBean() {
		return temBean;
	}

	public void setTemBean(TemsilciBean temBean) {
		this.temBean = temBean;
	}

	public boolean isHataCzIslem() {
		return hataCzIslem;
	}

	public void setHataCzIslem(boolean hataCzIslem) {
		this.hataCzIslem = hataCzIslem;
	}
}